package com.ymatou.iapi.messageroute.testcase;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.messageroute.parameter.BizTypeEnum;
import com.ymatou.iapi.messageroute.parameter.SendTradingMsgBean;
import com.ymatou.iapi.messageroute.service.SendTradingMsgCall;
import com.ymatou.iapi.messageroute.service.SendTradingMsgCallNet;
import com.ymatou.iapi.messageroute.service.SendTradingMsgService;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.sqlwapper.DefaultWapper;

/**
 *  消息路由接受MQ消息接口
 */
@TestSuite
public class Ts_SendTradingMsg {

	private static SendTradingMsgBean sendTradingMsgBean;
	private static SendTradingMsgCall sendTradingMsgCall;
	private static SendTradingMsgCallNet sendTradingMsgCallNet;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("消息路由接受MQ消息接口");

	}

	@Before
	public void caseUp() {
		sendTradingMsgBean = new SendTradingMsgBean();
		sendTradingMsgCall = new SendTradingMsgCall();
		sendTradingMsgCallNet = new SendTradingMsgCallNet();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult("");
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	//.net 有bug，手工校验
	public void Tc_SendTradingMsg_001() {
		Logger.start(true, "ACCEPT_ORDER 卖家已接单(用户已上传身份证)");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.acceptOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();

			sendTradingMsgBean.setBusinessType(BizTypeEnum.ACCEPT_ORDER
					.getCode());
			sendTradingMsgBean.setBuyerId(userId);
			sendTradingMsgBean.setOrderId(orderId);

			Map<String, String> map = new HashMap<String, String>();

			map.put("ProductName", "自动化测试商品+");
			map.put("NeedIdCard", "false");
			map.put("HasIdCard", "true");

			sendTradingMsgBean.setParamExt(map);
			sendTradingMsgCall.setData(sendTradingMsgBean);
			sendTradingMsgCallNet.setData(sendTradingMsgBean);

			sendTradingMsgCall.callService();
			sendTradingMsgCallNet.callService();

			Logger.verifyEquals("ok", sendTradingMsgCall.getReturnData(),
					"验证接口返回");
			Logger.verifyEquals("\"ok\"",
					sendTradingMsgCallNet.getReturnData(), "验证老.net接口返回");
			// 验证推送消息
			SendTradingMsgService.verfiyWithNet(BizTypeEnum
					.findByCode(sendTradingMsgBean.getBusinessType()),
					sendTradingMsgBean.getBuyerId(), sendTradingMsgBean
							.getOrderId());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendTradingMsg_002() {
		Logger.start(true, "NO_PAY_NOTIFY 订单未支付自动取消前10分钟通知 ");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService
					.placeOrderWithOutCoupon();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();

			int mainOrderId = placeOrderResp.getTradingId();

			sendTradingMsgBean.setBusinessType(BizTypeEnum.NO_PAY_NOTIFY
					.getCode());
			sendTradingMsgBean.setBuyerId(userId);
			sendTradingMsgBean.setOrderId(orderId);

			Map<String, String> map = new HashMap<String, String>();

			map.put("ProductName", "自动化测试商品+");
			map.put("MainOrderId", String.valueOf(mainOrderId));

			sendTradingMsgBean.setParamExt(map);
			sendTradingMsgCall.setData(sendTradingMsgBean);
			sendTradingMsgCallNet.setData(sendTradingMsgBean);

			sendTradingMsgCall.callService();
			sendTradingMsgCallNet.callService();

			Logger.verifyEquals("ok", sendTradingMsgCall.getReturnData(),
					"验证接口返回");
			Logger.verifyEquals("\"ok\"",
					sendTradingMsgCallNet.getReturnData(), "验证老.net接口返回");
			// 验证推送消息
			SendTradingMsgService.verfiyWithNet(BizTypeEnum
					.findByCode(sendTradingMsgBean.getBusinessType()),
					sendTradingMsgBean.getBuyerId(), sendTradingMsgBean
							.getOrderId());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendTradingMsg_003() {
		Logger.start(true, "RECEIVE_NOTICE 订单自动确认收货前通知(需要提醒上传身份证)");
		try {

			//{"OrderId":112523884,"BizID":112523884,"BuyerId":20238699,"SellerId":3383,"IsShangouOrder":false,"BusinessType":5}

			PlaceOrderResp placeOrderResp = PlaceOrderService.dispatchedOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();

			sendTradingMsgBean.setBusinessType(BizTypeEnum.RECEIVE_NOTICE
					.getCode());
			sendTradingMsgBean.setBuyerId(userId);
			sendTradingMsgBean.setOrderId(orderId);

			Map<String, String> map = new HashMap<String, String>();

			sendTradingMsgBean.setParamExt(map);
			sendTradingMsgCall.setData(sendTradingMsgBean);
			sendTradingMsgCallNet.setData(sendTradingMsgBean);

			sendTradingMsgCall.callService();
			sendTradingMsgCallNet.callService();

			Logger.verifyEquals("ok", sendTradingMsgCall.getReturnData(),
					"验证接口返回");
			Logger.verifyEquals("\"ok\"",
					sendTradingMsgCallNet.getReturnData(), "验证老.net接口返回");
			// 验证推送消息
			SendTradingMsgService.verfiyWithNet(BizTypeEnum
					.findByCode(sendTradingMsgBean.getBusinessType()),
					sendTradingMsgBean.getBuyerId(), sendTradingMsgBean
							.getOrderId());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SendTradingMsg_004() {
		Logger.start(true, "RECEIVE_NOTICE 订单自动确认收货前通知(不需要提醒上传身份证)");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.dispatchedOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();

			Thread.sleep(2000);

			sendTradingMsgBean.setBusinessType(BizTypeEnum.RECEIVE_NOTICE
					.getCode());
			sendTradingMsgBean.setBuyerId(userId);
			sendTradingMsgBean.setOrderId(orderId);

			Map<String, String> map = new HashMap<String, String>();

			sendTradingMsgBean.setParamExt(map);
			sendTradingMsgCall.setData(sendTradingMsgBean);
			sendTradingMsgCallNet.setData(sendTradingMsgBean);

			new DefaultWapper()
					.executeUpdateSql("update ymt_orderext set bIsNeedUploadIdCard=0 , bHaveUploadedIdCard=0 where iOrderId="
							+ orderId);

			sendTradingMsgCall.callService();
			sendTradingMsgCallNet.callService();

			Logger.verifyEquals("ok", sendTradingMsgCall.getReturnData(),
					"验证接口返回");
			Logger.verifyEquals("\"ok\"",
					sendTradingMsgCallNet.getReturnData(), "验证老.net接口返回");
			// 验证推送消息
			SendTradingMsgService.verfiyWithNet(BizTypeEnum
					.findByCode(sendTradingMsgBean.getBusinessType()),
					sendTradingMsgBean.getBuyerId(), sendTradingMsgBean
							.getOrderId());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SendTradingMsg_005() {
		Logger.start(true, "同一个消息重复请求");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.dispatchedOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();

			sendTradingMsgBean.setBusinessType(BizTypeEnum.RECEIVE_NOTICE
					.getCode());
			sendTradingMsgBean.setBuyerId(userId);
			sendTradingMsgBean.setOrderId(orderId);

			Map<String, String> map = new HashMap<String, String>();

			map.put("ProductName", "王凯测试商品 3次修改");
			map.put("NeedIdCard", "false");
			map.put("HasIdCard", "true");

			sendTradingMsgBean.setParamExt(map);
			sendTradingMsgCall.setData(sendTradingMsgBean);

			sendTradingMsgCall.callService();

			Thread.sleep(2000);

			sendTradingMsgCall.callService();

			Logger.verifyEquals("ok", sendTradingMsgCall.getReturnData(),
					"验证接口返回");

			// 验证推送消息

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	//手工校验
	public void Tc_SendTradingMsg_006() {
		Logger.start(true, "消息推送渠道失败，在定时服务生成一条补单消息");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.payOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();

			sendTradingMsgBean.setBusinessType(BizTypeEnum.ACCEPT_ORDER
					.getCode());
			sendTradingMsgBean.setBuyerId(userId);
			sendTradingMsgBean.setOrderId(orderId);

			Map<String, String> map = new HashMap<String, String>();

			map.put("ProductName", "王凯测试商品 3次修改");
			map.put("NeedIdCard", "false");
			map.put("HasIdCard", "true");

			sendTradingMsgBean.setParamExt(map);
			sendTradingMsgCall.setData(sendTradingMsgBean);

			sendTradingMsgCall.callService();

			Logger.verifyEquals("ok", sendTradingMsgCall.getReturnData(),
					"验证接口返回");

			// 验证推送消息
			/*			SendTradingMsgService.verfiyWithNet(BizTypeEnum
								.findByCode(sendTradingMsgBean.getBusinessType()),
								sendTradingMsgBean.getBuyerId(), sendTradingMsgBean
										.getOrderId());*/

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	public void Tc_SendTradingMsg_007() {
		Logger.start(true, "发货");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.acceptOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			int userId = placeOrderResp.getUserId();

			sendTradingMsgBean
					.setBusinessType(BizTypeEnum.DISPATCHED.getCode());
			sendTradingMsgBean.setBuyerId(userId);
			sendTradingMsgBean.setOrderId(orderId);

			Map<String, String> map = new HashMap<String, String>();

			sendTradingMsgBean.setParamExt(map);
			sendTradingMsgCall.setData(sendTradingMsgBean);

			sendTradingMsgCall.callService();

			Logger.verifyEquals("ok", sendTradingMsgCall.getReturnData(),
					"验证接口返回");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
